package lagouHomeWork;

/**
 * @author StarryHorizon
 * @create 2022-01-04 16:57
 */
public class threadHomeWork3 extends Thread{

    //描述火车票
    private static int tickets = 1;

    public threadHomeWork3(int tickets) {
        this.tickets = tickets;
    }

    public threadHomeWork3() {
    }

    public int getTickets() {
        return tickets;
    }

    public void setTickets(int tickets) {
        this.tickets = tickets;
    }

    @Override
    public void run() {
        synchronized (this) {
        while (true) {
                //模拟100张火车票取票过程
                if (tickets < 99) {
                    System.out.println("窗口" + Thread.currentThread().getName() + "正在取票");
                    System.out.println(tickets);
                    tickets++;

                } else {
                    System.out.println("火车票已取完");
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {

        //模拟3个线程
        threadHomeWork3 h1 = new threadHomeWork3();
        Thread t1 = new Thread(h1);
        threadHomeWork3 h2 = new threadHomeWork3();
        Thread t2 = new Thread(h2);
        threadHomeWork3 h3 = new threadHomeWork3();
        Thread t3 = new Thread(h3);

        //启动线程
        System.out.println("开始取票");
        t1.start();
        t2.start();
        t3.start();

    }
}
